package com.blockmeta.bbs.overallserviceapplication.search.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.adapter.BBSListAdapter;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.base.fragment.BaseLazyBusinessListFragment;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.pojo.BBSNewestListPOJO;
import com.blockmeta.bbs.overallserviceapplication.R;
import com.blockmeta.bbs.businesslibrary.pojo.BBSListZanPOJO;
import com.blockmeta.bbs.overallserviceapplication.popupview.BBSChoosePopup;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.HashMap;

/**
 * Created by author on 2018/5/8.
 */
@Route(path = RouterPath.BBS_LIST_FRAGMENT)
public class BBSListFragment extends BaseLazyBusinessListFragment<BBSListZanPOJO> implements View.OnClickListener {
    private int pageNo = 1;
    private BBSChoosePopup mChoosePopup;
    private TextView mTvChoose;
    private String sortRuler = "";

    @Autowired(name = Constant.ZERO)
    public String mKey;
    @Autowired(name = Constant.ONE)
    public String mValue;
    @Autowired(name = Constant.THREE)
    public boolean mIM;

    public static BBSListFragment newInstance(String key, String value) {
        BBSListFragment fragment = new BBSListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.ZERO, key);
        bundle.putString(Constant.ONE, value);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public BaseQuickAdapter initAdapter() {
        return new BBSListAdapter(null, mContext);
    }

    @Override
    public String getUrl() {
        if ("keyword".equals(getArguments().getString(Constant.ZERO))){
            return NetConstant.BBS_ACTIVE_SEARCH;
        }
        return NetConstant.BBS_LIST;
    }

    @Override
    public HashMap<String, String> initHashMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put("page", getPageNo("1"));
        map.put(getArguments().getString(Constant.ZERO), getArguments().getString(Constant.ONE));
        map.put("orderby", sortRuler);
        if (mTvChoose != null) {
            switch (sortRuler) {
                case "lastpost":
                    mTvChoose.setText("回复时间排序");
                    break;
                case "dateline":
                    mTvChoose.setText("发帖时间排序");
                    break;
                case "replies":
                    mTvChoose.setText("评论数量排序");
                    break;
                case "getViews":
                    mTvChoose.setText("浏览数量排序");
                    break;
                default:
                    break;
            }
        }
        ((BBSListAdapter)mAdapter).setSearchKeyWord(getArguments().getString(Constant.ONE));
        return map;
    }

    @Override
    public BBSListZanPOJO parseData(String resp) {
        BBSNewestListPOJO pojo = GsonUtil.buildGson().fromJson(resp, BBSNewestListPOJO.class);
        return pojo.data;
    }

    @Override
    public View addHeaderView() {
        if (getArguments().getString(Constant.ZERO).equals("fid") && getArguments().getString(Constant.ONE).equals("61")
                ||("keyword".equals(getArguments().getString(Constant.ZERO)))) {
            return null;
        } else {
            return chooseHeaderView();
        }
    }

    /**
     * 论坛需要添加排序筛选功能
     */
    private View chooseHeaderView() {
        View chooseHeader = LayoutInflater.from(mContext).inflate(R.layout.header_choose_bbs, null);
        mTvChoose = chooseHeader.findViewById(R.id.choose_rule);
        switch (sortRuler) {
            case "lastpost":
                mTvChoose.setText("回复时间排序");
                break;
            case "dateline":
                mTvChoose.setText("发帖时间排序");
                break;
            case "replies":
                mTvChoose.setText("评论数量排序");
                break;
            case "getViews":
                mTvChoose.setText("浏览数量排序");
                break;
            default:
                break;
        }

        chooseHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSortPopup();
            }
        });

        return chooseHeader;
    }

    private void showSortPopup() {
        mChoosePopup = new BBSChoosePopup(mContext);
        mChoosePopup.replyTime.setOnClickListener(this);
        mChoosePopup.sendTime.setOnClickListener(this);
        mChoosePopup.commentNum.setOnClickListener(this);
        mChoosePopup.skinNum.setOnClickListener(this);
        mChoosePopup.cancle.setOnClickListener(this);
        mChoosePopup.showPopupWindow();
    }


    @Override
    protected void adapterSetData() {
        //统计第一条数据

//        if (this.getParentFragment() instanceof BBSChainBarFragment){
//           if (((BBSChainBarFragment)this.getParentFragment()).tab.getSelectedTabPosition()==0){
//               HashMap<String, String> map = new HashMap<>();
//               try {
//                   map.put("firstBBS", String.valueOf(mPOJO.getList().get(0).getTid()));
//               } catch (Exception e) {
//               }
//               UmengEvent.eventStatistics(mContext, Constant.U_BBS_banner, map);
//           }
//        }



        mAdapter.setNewData(mPOJO.getList());
    }

    @Override
    protected void adapterAddData() {
        if (mPOJO.getList() == null) {
            list_swipe.finishLoadmore();
            return;
        }
        mAdapter.addData(mPOJO.getList());
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        if (mPOJO != null && mPOJO.getList() != null) {
            pageNo++;
            setPageNo(String.valueOf(pageNo));
            initNet(true);
        } else {
            list_swipe.finishLoadmore();
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        setPageNo("1");
        pageNo = 1;
        initNet(false);
    }

    /**
     * 排序规则
     *
     * @param view "orderby":'',
     *             可选,排序方式,默认按回帖时间最新(或论坛后台设置)
     *             'getViews','replies','dateline','lastpost','displayorder','heats'
     *             浏览量,回帖量,发表时间,最后回复时间,置顶排序,热度(最热主题)z
     */
    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.bbs_tv1) {
            mTvChoose.setText("回复时间排序");
            sortRuler = mContext.getResources().getString(R.string.zbbs_sort_reply);
            setPageNo("1");
            pageNo = 1;
            initNet(false);

        } else if (i == R.id.bbs_tv2) {
            mTvChoose.setText("发帖时间排序");
            sortRuler = mContext.getResources().getString(R.string.bbs_sort_send);
            setPageNo("1");
            pageNo = 1;
            initNet(false);

        } else if (i == R.id.bbs_tv3) {
            mTvChoose.setText("评论数量排序");
            sortRuler = mContext.getResources().getString(R.string.bbs_sort_comment);
            setPageNo("1");
            pageNo = 1;
            initNet(false);

        } else if (i == R.id.bbs_tv4) {
            mTvChoose.setText("浏览数量排序");
            sortRuler = mContext.getResources().getString(R.string.bbs_sort_skin);
            setPageNo("1");
            pageNo = 1;
            initNet(false);

        } else if (i == R.id.bbs_cancle) {
        }
        setPageNo("1");
        if (mChoosePopup != null) {
            mChoosePopup.dismiss();
        }
    }
}
